/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.ens.delegation.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.ens.delegation.bo.Commune;
import org.ens.delegation.dao.ICommuneDao;

/**
 *
 * @author amine
 */
public class CommuneAction extends GenericAction{

    private Commune commune;
    private List listCommunePag;
    
    private String[] columnsSort = { "", "descriptionAr", "descriptionFr","region.descriptionFr"};
    
    private Map _jsonPaginator;
    private Map jsonResult;
    private Map jsonData;
       
    private String start;
    private String length;
    private String sort;
    private String sortdir;
    private String echo;

    private String strDescriptionAr;
    private String strDescriptionFr;
    private String strRegion;


    private ICommuneDao communeDao;
    
    public CommuneAction() {
    }

    public CommuneAction(ICommuneDao communeDao) {
        this.communeDao = communeDao;
    }
    
    @Override
    public String execute() throws Exception {
        setCommune(null);
        setActionMessages(null);
        return SUCCESS;
    }
    
    @Override
    public String searchPaginator(){
        
        setJsonPaginator(new HashMap<String,Object>());
        
        setListCommunePag(getCommuneDao().findByFiltersWithFirstMaxResult(Integer.valueOf(getStart()), Integer.valueOf(getLength()), getColumnsSort()[Integer.valueOf(getSort())], getSortdir(), strDescriptionAr, strDescriptionFr, strRegion));
        
        getJsonPaginator().put("sEcho", getEcho());
        
        Integer rowCount = getCommuneDao().getRowCountByFilters(getStrDescriptionAr(),getStrDescriptionFr(),getStrRegion());
        
        getJsonPaginator().put("iTotalRecords", String.valueOf(rowCount));
        
        if (Integer.valueOf(getLength()).compareTo(rowCount) > 0) {
            getJsonPaginator().put("iTotalDisplayRecords", getLength());
        } else {
            getJsonPaginator()
                    .put("iTotalDisplayRecords", String.valueOf(rowCount));
        }
        
        String[][] aaData = new String[getListCommunePag().size()][];
        for (int i = 0; i < getListCommunePag().size(); i++) {
            Commune co = (Commune) getListCommunePag().get(i);
            String s [] = new String[5];
            s[0] = "<input type=\"checkbox\" name=" + co.getId()
                    + "></input>";
            
            s[1] = co.getDescriptionFr();
            s[2] = co.getDescriptionAr();
            s[3] = co.getRegion().getDescriptionFr();

            s[4] = "<a href=\"getCommunesMod.go?ids=" + co.getId()
                    + "\"><img src=\"./gfx/icon-edit.png\" alt=\"edit\" /></a>";
            aaData[i] = s;
        }
        
        getJsonPaginator().put("aaData", aaData);
        return SUCCESS;
    }

    @Override
    public String add() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public String edit() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public String save() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public String saveNew() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public String delete() {
        throw new UnsupportedOperationException("Not supported yet.");
    }


    /**
     * @return the _jsonPaginator
     */
    public Map get_jsonPaginator() {
        return getJsonPaginator();
    }

    /**
     * @param jsonPaginator the _jsonPaginator to set
     */
    public void set_jsonPaginator(Map jsonPaginator) {
        this.setJsonPaginator(jsonPaginator);
    }

    /**
     * @return the _jsonPaginator
     */
    public Map getJsonPaginator() {
        return _jsonPaginator;
    }

    /**
     * @param jsonPaginator the _jsonPaginator to set
     */
    public void setJsonPaginator(Map jsonPaginator) {
        this._jsonPaginator = jsonPaginator;
    }

    /**
     * @return the jsonResult
     */
    public Map getJsonResult() {
        return jsonResult;
    }

    /**
     * @param jsonResult the jsonResult to set
     */
    public void setJsonResult(Map jsonResult) {
        this.jsonResult = jsonResult;
    }

    /**
     * @return the jsonData
     */
    public Map getJsonData() {
        return jsonData;
    }

    /**
     * @param jsonData the jsonData to set
     */
    public void setJsonData(Map jsonData) {
        this.jsonData = jsonData;
    }

    /**
     * @return the start
     */
    public String getStart() {
        return start;
    }

    /**
     * @param start the start to set
     */
    public void setStart(String start) {
        this.start = start;
    }

    /**
     * @return the length
     */
    public String getLength() {
        return length;
    }

    /**
     * @param length the length to set
     */
    public void setLength(String length) {
        this.length = length;
    }

    /**
     * @return the sort
     */
    public String getSort() {
        return sort;
    }

    /**
     * @param sort the sort to set
     */
    public void setSort(String sort) {
        this.sort = sort;
    }

    /**
     * @return the sortdir
     */
    public String getSortdir() {
        return sortdir;
    }

    /**
     * @param sortdir the sortdir to set
     */
    public void setSortdir(String sortdir) {
        this.sortdir = sortdir;
    }

    /**
     * @return the echo
     */
    public String getEcho() {
        return echo;
    }

    /**
     * @param echo the echo to set
     */
    public void setEcho(String echo) {
        this.echo = echo;
    } 
  
    /**
     * @return the columnsSort
     */
    public String[] getColumnsSort() {
        return columnsSort;
    }

    /**
     * @param columnsSort the columnsSort to set
     */
    public void setColumnsSort(String[] columnsSort) {
        this.columnsSort = columnsSort;
    }

    /**
     * @return the commune
     */
    public Commune getCommune() {
        return commune;
    }

    /**
     * @param commune the commune to set
     */
    public void setCommune(Commune commune) {
        this.commune = commune;
    }

    /**
     * @return the communeDao
     */
    public ICommuneDao getCommuneDao() {
        return communeDao;
    }

    /**
     * @param communeDao the communeDao to set
     */
    public void setCommuneDao(ICommuneDao communeDao) {
        this.communeDao = communeDao;
    }

    /**
     * @return the listCommunePag
     */
    public List getListCommunePag() {
        return listCommunePag;
    }

    /**
     * @param listCommunePag the listCommunePag to set
     */
    public void setListCommunePag(List listCommunePag) {
        this.listCommunePag = listCommunePag;
    }

    /**
     * @return the strDescriptionAr
     */
    public String getStrDescriptionAr() {
        return strDescriptionAr;
    }

    /**
     * @param strDescriptionAr the strDescriptionAr to set
     */
    public void setStrDescriptionAr(String strDescriptionAr) {
        this.strDescriptionAr = strDescriptionAr;
    }

    /**
     * @return the strDescriptionFr
     */
    public String getStrDescriptionFr() {
        return strDescriptionFr;
    }

    /**
     * @param strDescriptionFr the strDescriptionFr to set
     */
    public void setStrDescriptionFr(String strDescriptionFr) {
        this.strDescriptionFr = strDescriptionFr;
    }

    /**
     * @return the strRegion
     */
    public String getStrRegion() {
        return strRegion;
    }

    /**
     * @param strRegion the strRegion to set
     */
    public void setStrRegion(String strRegion) {
        this.strRegion = strRegion;
    }


    
    
}
